/*
 * Copyright 2008 the original author Ang Gao.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * SearchResultView.java 2008-11-11
 */
package ie.ucc.cs.anggao.finalproject.client.ui.mainpanel.infopane;

import ie.ucc.cs.anggao.finalproject.client.model.SearchEngine;
import ie.ucc.cs.anggao.finalproject.client.model.SearchEngineResult;
import ie.ucc.cs.anggao.finalproject.client.support.css.CSSManager;
import ie.ucc.cs.anggao.finalproject.client.support.searchengine.ResultLink;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author GaoAng
 * 
 */
public class SearchResultView extends Composite {

	private DisclosurePanel mainPanel = null;
	private VerticalPanel results = new VerticalPanel();
	private SearchEngine engine;

	public SearchResultView(SearchEngine engine) {
		this.engine = engine;
		mainPanel = new DisclosurePanel(engine.getName());
		initWidget(mainPanel);
		mainPanel.setContent(results);
		mainPanel.setStyleName(CSSManager.SearchResultsView);

	}

	public void clearResults() {
		results.clear();
		mainPanel.setOpen(true);
	}

	public void addSearchResult(final SearchEngineResult result, int i) {
		VerticalPanel resultItem = new VerticalPanel();
		ResultLink title = new ResultLink(result.getTitle(), result.getUrl());
		HTML description = new HTML(result.getDescription());

		description.addClickListener(new ClickListener() {
			public void onClick(Widget w) {
				PopupPanel p = new PopupPanel(true);
				// p.addStyleName("keepPopup");
				p.setWidget(getImage(result.getUrl()));
				p.setPopupPosition(w.getAbsoluteLeft() + 5, w.getAbsoluteTop());
				p.show();
			}
		});
		
		final int id = i;
		//Listener for testing purpose
		title.addClickListener(new ClickListener(){

			public void onClick(Widget sender) {
//				Window.open("/count?pos="+id, "newWindowName",
//				"window features.");
				
				RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,"/count?pos="+id);
				try {
					builder.sendRequest(null,new RequestCallback(){
						public void onError(Request request, Throwable exception) {
							
						}

						public void onResponseReceived(Request request,
								Response response) {
							
						}
						
					});
				} catch (RequestException e) {
					
					e.printStackTrace();
				}
			}
			
		});
		
		resultItem.add(title);
		resultItem.add(description);
		resultItem.addStyleName(CSSManager.ResultItem);
		title.setStyleName(CSSManager.searchResultLink);
		description.setStyleName(CSSManager.searchResultDescription);

		results.add(resultItem);

	}

	public Widget getImage(String url) {

		Image image = new Image();
		image.setUrl("http://open.thumbshots.org/image.aspx?url=" + url);
		return image;

	}

	public SearchEngine getSearchEngine() {
		return this.engine;
	}

}
